using TMPro;
using UnityEngine;

public class UI_ItemToolTip : UI_ToolTip
{
    [SerializeField] private TextMeshProUGUI itemName;
    [SerializeField] private TextMeshProUGUI itemType;
    [SerializeField] private TextMeshProUGUI itemDescript;
    [SerializeField] private TextMeshProUGUI itemPrice;
    [SerializeField] private Transform opeaDetails;

    public void ShowToolTip(bool isShow, RectTransform targetRect, Inventory_Item itemToShow, bool isBuy = false, bool isDisplay = false)
    {
        base.ShowToolTip(isShow, targetRect);
        if (itemToShow == null)
            return;

        itemPrice.text = "";

        if (isDisplay)
        {
            string totalBuyPrice = itemToShow.stackSize > 1 ?
            itemToShow.price + "x" + itemToShow.stackSize + "=" + itemToShow.price * itemToShow.stackSize :
            itemToShow.price.ToString();

            string totalSellPrice = itemToShow.stackSize > 1 ?
            itemToShow.sellPrice + "x" + itemToShow.stackSize :
            itemToShow.sellPrice.ToString();

            string priceText = isBuy ? "Price: " + totalBuyPrice + "g" : "Sell Price: " + totalSellPrice + "g";

            itemPrice.text = priceText;
        }

        opeaDetails.gameObject.SetActive(isDisplay);
        
        itemName.text = GetColorText(GetColorByRarity(itemToShow.itemData.itemRarity),itemToShow.itemData.itemName);
        itemType.text = itemToShow.itemData.itemType.ToString();
        itemDescript.text = itemToShow.GetDescription();
    }

    private string GetColorByRarity(float rarity)
    {
        if (rarity <= 100) return "white";
        if (rarity <= 300) return "green";
        if (rarity <= 600) return "blue";
        if (rarity <= 600) return "purple";

        return "orange";
    }

    private string GetColorText(string color, string text) => $"<color=\"{color}\">{text}</color>";
}
